package com.example.onepeak.androiddemo.tab;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.onepeak.androiddemo.R;
import com.example.onepeak.androiddemo.common.SimpleAdapter;

import java.util.ArrayList;
import java.util.List;

public class SuspensionTabActivity extends AppCompatActivity implements TabScrollView.OnScrollListener{

    private ImageView iv_top;
    private TabScrollView tabScrollView;
    private LinearLayout ll_tab;
    private RecyclerView rv_list;
    private ListRvAdapter listRvAdapter;
    private View view;

    private int picBottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suspension_tab);

        rv_list = (RecyclerView) findViewById(R.id.rv_list);
        rv_list.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        rv_list.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL));
        rv_list.setFocusable(false);

        ll_tab = (LinearLayout) findViewById(R.id.ll_tab);
        iv_top = (ImageView) findViewById(R.id.iv_top);
        tabScrollView = (TabScrollView) findViewById(R.id.tabScrollView);
        view = findViewById(R.id.view);
        tabScrollView.setOnScrollListener(this);

        LinearLayout ll_content = (LinearLayout)findViewById(R.id.ll_content);
        ll_content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                onScroll(tabScrollView.getScrollY());
            }
        });

        List<String> data = new ArrayList<>();
        for(int i=0;i<30;i++){
            data.add("哈哈哈哈或或或或或或或或或"+i);
        }

        listRvAdapter = new ListRvAdapter(this,R.layout.item_text);
        listRvAdapter.setDataSets(data);
        rv_list.setAdapter(listRvAdapter);
        listRvAdapter.notifyDataSetChanged();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            picBottom = iv_top.getBottom();
        }
    }

    @Override
    public void onScroll(int scrollY) {
        int top = Math.max(scrollY, picBottom);
        Log.e("onScroll","scrollY = " +scrollY + " , picBottom = " + picBottom);
        ll_tab.layout(0, top, view.getWidth(), top + view.getHeight());
    }

    class ListRvAdapter extends SimpleAdapter<String>  {

        public ListRvAdapter(Context context, int layoutId) {
            super(context, layoutId);
        }

        @Override
        protected void OnBindView(ViewHolder holder, String item, int position) {
            ((TextView)holder.findViewById(R.id.tv_text)).setText(item);
        }
    }
}
